package com.example.view;

import com.example.entry.DeviceEntry;
import com.example.draw.Grid;
import com.example.draw.Rect;
import com.example.utils.PropertiesUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.File;
/**
 * @author:我叫程晓非
 * @desc:截图JPanel
 * @date:2021-11-08
 * @version:1.0
 */
public class DisplayJPanel extends JPanel implements MouseListener, MouseMotionListener {
    public static float scale = 0.0f;
    public static int padding = 20;
    public static int x;
    public static int y;
    public static int width;
    public static int height;
    public static int PANEL_WIDTH;
    public static int PANEL_HEIGHT;
    int gridX;
    int gridY;
    int gridWidth;
    int gridHeight;

    int rectX;
    int rectY;
    int rectHeight = 20;
    int rectWidth = 20;

    boolean isFlush = false;
    int level = 1;
    Image img = null;
    private int imgWidth;
    private int imgHeight;

    private DeviceEntry<String,String> deviceEntry;
    // 定位网格
    private final Grid grid;
    // 定位撤销框
    private final Rect rect;
    private static final String CONFIG_FILE = "./"+MainFrame.USER_CONFIG_FOLDER+"/"+"default"+MainFrame.USER_CONFIG_SUFFIX;
    public DisplayJPanel(int level){//construct
        super();

        addMouseListener(this);
        addMouseMotionListener(this);

        //读取配置
        String gridXStr = PropertiesUtil.readValue(CONFIG_FILE,"grid.x");
        String gridYStr = PropertiesUtil.readValue(CONFIG_FILE,"grid.y");
        String gridWidthStr = PropertiesUtil.readValue(CONFIG_FILE,"grid.width");
        String gridHeightStr = PropertiesUtil.readValue(CONFIG_FILE,"grid.height");


        String rectXStr = PropertiesUtil.readValue(CONFIG_FILE,"rect.x");
        String rectYStr = PropertiesUtil.readValue(CONFIG_FILE,"rect.y");
        String rectWidthStr = PropertiesUtil.readValue(CONFIG_FILE,"rect.width");
        String rectHeightStr = PropertiesUtil.readValue(CONFIG_FILE,"rect.height");

        gridWidth = Integer.parseInt(gridWidthStr);
        gridHeight = Integer.parseInt(gridHeightStr);
        gridX = Integer.parseInt(gridXStr);
        gridY = Integer.parseInt(gridYStr);

        rectX = Integer.parseInt(rectXStr);
        rectY = Integer.parseInt(rectYStr);

        grid = new Grid(level,gridX,gridY,gridHeight,gridWidth,new Color(1,1,1));

        rect = new Rect(rectX,rectY,rectWidth,rectHeight,new Color(1,1,1));


    }

    public Rect getRect() {
        return rect;
    }

    public void setDeviceEntry(DeviceEntry<String,String> deviceEntry){
        this.deviceEntry = deviceEntry;
        isFlush = true;
        repaint();
    }

    public void setRectWidth(int rectWidth){
        this.rectWidth = rectWidth;
        this.rect.setWidth(rectWidth);
        repaint();
    }

    public Image getImg(){
        return this.img;
    }
    public void setRectHeight(int rectHeight){
        this.rectHeight = rectHeight;
        this.rect.setHeight(rectHeight);
        repaint();
    }

    @Override
    public void paint(Graphics g) {

        super.paint(g);

        if(deviceEntry!=null &&!"0".equals(this.deviceEntry.getKey())){
            long start = System.currentTimeMillis();
            File file = new File("./"+MainFrame.USER_SCREENSHOT_FOLDER+"/"+this.deviceEntry.getKey()+".png");
            if(!file.exists()){
                System.out.println("图片文件不存在");
                return;
            }

            img =  Toolkit.getDefaultToolkit().getImage(file.getAbsolutePath());

            int imageWidth = img.getWidth(this);
            int imageHeight = img.getHeight(this);
            int panelWidth = this.getWidth();
            int panelHeight = this.getHeight();
//            int panelWidth = 422;
//            int panelHeight = 691;
            height = panelHeight - 2*padding;
            //这里丢失了精度
            scale = imageHeight/(float)height;
            width = (int)(imageWidth/scale);
            imgWidth = (int)(imageWidth/scale);
            imgHeight = panelHeight - 2*padding;
            y = padding;
            x = (panelWidth - width)/2;
            g.drawImage(img,x,y,imgWidth,imgHeight,this);
            long end = System.currentTimeMillis();
//            System.out.printf("x = %d,y = %d,imageWidth = %d,imageHeight = %d,scale = %.2f,time = %d\n",x,y,imageWidth,imageHeight,scale,(end-start));
            if(img != null && isFlush){
                img.flush();
                isFlush = false;
            }
        }
        grid.draw(g);
        rect.draw(g);

    }
    public int getImageX(){
        return x;
    }
    public int getImageY(){
        return y;
    }
    public float getScale(){
        return scale;
    }
    public void paintComponent(Graphics g){
        super.paintComponent(g);

        if(this.deviceEntry!=null){
            ImageIcon icon=new ImageIcon("./"+MainFrame.USER_SCREENSHOT_FOLDER+"/"+this.deviceEntry.getKey()+".png");
            g.drawImage(icon.getImage(), x,y, width, height, this);
        }


    }

    private boolean isSelectedGrid = false;
    private boolean isSelectedRect = false;
    private int startX;
    private int startY;
    private int deltaX;
    private int deltaY;
    @Override
    public void mouseClicked(MouseEvent e) {
        Point point = e.getPoint();
        System.out.println(point.x+","+point.y);
        startX = point.x;
        startY = point.y;
        Rectangle gridRectangle = getGridRect();

        Rectangle rectRectangle = getRectangleRect();
        if(gridRectangle.contains(point.x,point.y)){

            deltaX = this.grid.getX() - startX;
            deltaY = this.grid.getY() - startY;
            isSelectedGrid = true;
            isSelectedRect = false;
        }else{
            isSelectedGrid = false;
        }
        if(rectRectangle.contains(point.x,point.y)){

            deltaX = this.rect.getX() - startX;
            deltaY = this.rect.getY() - startY;
            isSelectedRect = true;
            isSelectedGrid = false;
        }else{
            isSelectedRect = false;

        }
        if(isSelectedRect){
            this.rect.setColor(new Color(255,0,0));
        }else{
            this.rect.setColor(new Color(1,1,1));
        }
        if(isSelectedGrid){
            this.grid.setColor(new Color(255,0,0));
        }else{
            this.grid.setColor(new Color(1,1,1));
        }
        repaint();
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

        Point point = e.getPoint();
        System.out.println(point.x+","+point.y);
        startX = point.x;
        startY = point.y;
        Rectangle gridRectangle = getGridRect();

        Rectangle rectRectangle = getRectangleRect();
        if(gridRectangle.contains(point.x,point.y)){

            deltaX = this.grid.getX() - startX;
            deltaY = this.grid.getY() - startY;
            isSelectedGrid = true;
            isSelectedRect = false;
        }else{
            isSelectedGrid = false;
        }
        if(rectRectangle.contains(point.x,point.y)){

            deltaX = this.rect.getX() - startX;
            deltaY = this.rect.getY() - startY;
            isSelectedRect = true;
            isSelectedGrid = false;
        }else{
            isSelectedRect = false;
        }
        if(isSelectedRect){
            this.rect.setColor(new Color(255,0,0));
        }else{
            this.rect.setColor(new Color(1,1,1));
        }
        if(isSelectedGrid){
            this.grid.setColor(new Color(255,0,0));
        }else{
            this.grid.setColor(new Color(1,1,1));
        }
        repaint();
    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

    public void setGridX(int x) {
        this.gridX = x;
        this.grid.setX(x,DisplayJPanel.x,this.imgWidth);
        repaint();
    }

    public Grid getGrid() {
        return grid;
    }

    public void setGridY(int y) {
        this.gridY = y;
        this.grid.setY(y,DisplayJPanel.y,this.imgHeight);
        repaint();
    }

    public void setGridWidth(int width) {
        this.gridWidth = width;
        this.grid.setWidth(width);
        repaint();

    }

    public void setGridHeight(int height) {
        this.gridHeight = height;
        this.grid.setHeight(height);
        repaint();
    }

    public void setLevel(int level) {
        this.level = level;
        this.grid.setLevel(level);
        repaint();
    }
    //用来判断是否选中grid
    public Rectangle getGridRect(){
        return new Rectangle(this.grid.getX(),this.grid.getY(),this.grid.getWidth(),this.grid.getHeight());
    }
    public Rectangle getRectangleRect(){
        return new Rectangle(this.rect.getX(),this.rect.getY(),this.rect.getWidth(),this.rect.getHeight());
    }

    public int getRectX() {
        return rectX;
    }

    public void setRectX(int rectX) {
        this.rectX = rectX;
        this.rect.setX(rectX,x,this.imgWidth);
        repaint();
    }

    public int getRectY() {
        return rectY;
    }

    public void setRectY(int rectY) {
        this.rectY = rectY;
        this.rect.setY(rectY,x,this.imgHeight);
        repaint();
    }
    //拖拽功能
    @Override
    public void mouseDragged(MouseEvent e) {
        Point point = e.getPoint();

        if(isSelectedGrid){
            int dx = point.x +deltaX;
            int dy = point.y +deltaY;
            this.grid.setX(dx,x,this.imgWidth);
            this.grid.setY(dy,y,this.imgHeight);
            repaint();
        }
        if(isSelectedRect){
            int dx = point.x +deltaX;
            int dy = point.y +deltaY;
            this.rect.setX(dx,x,this.imgWidth);
            this.rect.setY(dy,y,this.imgHeight);
            repaint();


        }
    }

    public boolean isSelectedGrid() {
        return isSelectedGrid;
    }

    public boolean isSelectedRect() {
        return isSelectedRect;
    }

    @Override
    public void mouseMoved(MouseEvent e) {

    }

    //双缓冲
//    private Image iBuffer;
//    private Graphics gBuffer;
//    @Override
//    public void update(Graphics g) {
//        if(iBuffer==null)
//        {
//            iBuffer=createImage(this.getSize().width,this.getSize().height);
//            gBuffer=iBuffer.getGraphics();
//        }
//        gBuffer.setColor(getBackground());
//        gBuffer.fillRect(0,0,this.getSize().width,this.getSize().height);
//        paint(g);
//        g.drawImage(iBuffer,0,0,this);
//    }


}
